import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.SQLException;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.naming.NamingException;

import unilearn.domain.*;
import unilearn.facade.*;

/**
 * @author Yin Zhang, 5393558
 * @version 0.1, 07 May 2008, initial creation
 */
public class StaffUserStory3
{
	private static PrintWriter fileOutput = null;
	private static final SimpleDateFormat DATEFORMAT = new SimpleDateFormat("dd/MM/yyyy 'at' HH:mm:ss");
	private static final String SEPARATELINE = "--------------------------------------------------------";
	private String staffID = null;
	private long submissionID = -1;
	private int[] criteriaGrades = null;
	private int finalMark = 0;
	private long assignmentID = -1;
	
	public StaffUserStory3(String[] args) throws FileNotFoundException
	{
		fileOutput = new PrintWriter(new FileOutputStream("staffAssessAssignment.txt"));
		staffID = args[0];
		submissionID = Long.parseLong(args[1]);
		criteriaGrades = new int[3];
		for(int counter = 0; counter < 3; counter++)
		{
			criteriaGrades[counter] = Integer.parseInt(args[counter + 2]);
			finalMark = finalMark + criteriaGrades[counter];
		}
	}
	
	public void assessAssignment() throws SQLException, NamingException
	{
		printAssignment();
		printSubmission();
		Assessment assessment = new Assessment();
		java.util.Date currentTime = new java.util.Date();
		Timestamp assessmentDate = new Timestamp(currentTime.getTime());
		assessment.setAssessmentDate(assessmentDate);
		assessment.setComment("Very Good!");
		assessment.setGrade(finalMark);
		assessment.setStaffID(staffID);
		assessment.setSubmissionID(submissionID);
		long assessmentID = AssessmentFacade.createNewAssessment(assessment);
		printCriteria(assessmentID);
		fileOutput.println(SEPARATELINE);
		fileOutput.println("Assessment Number "+ assessmentID + " added.");
	}
	
	private void printAssignment() throws SQLException, NamingException
	{
		Submission submission = SubmissionFacade.getSubmission(submissionID);
		Assignment assignment = AssignmentFacade.getAssignmentBySubmission(submission);
		
		assignment = AssignmentFacade.getAssignmentBySubmission(submission);
		String assignmentName = assignment.getName();
		
		Subject subject = SubjectFacade.getSubjectByID(assignment.getSubjectID());
		String subjectCode = subject.getSubjectCode();
		String subjectName = subject.getSubjectName();
		
		fileOutput.println("Subject: \t\t" + subjectCode + " - " + subjectName);
		fileOutput.println("Assessment: \t" + assignmentName);
		fileOutput.print(assignmentName);
		
		assignmentID = assignment.getID();
	}
	
	private void printSubmission() throws SQLException, NamingException, ArrayIndexOutOfBoundsException
	{
		Submission submission = SubmissionFacade.getSubmission(submissionID);
		
		String submittedDate = formatDate(submission.getDateSubmitted());
		
		fileOutput.print(" submitted on " + submittedDate);
		fileOutput.println();
	}
	
	private void printCriteria(long assessmentID) throws SQLException, NamingException
	{
		Vector<AssessmentCriteria> criterias = new Vector<AssessmentCriteria>();
		criterias = AssessmentCriteriaFacade.getByAssignmentID(assignmentID);
		int counter = 0;
		
		for(AssessmentCriteria criteria : criterias)
		{
			int criteriaMaxGrade = criteria.getCriteriaMaxGrade();
			long criteriaID = criteria.getCriteriaID();
			
			fileOutput.print(criteria.getCriteriaName());
			fileOutput.print("\t\t\t | \t\t\t");
			fileOutput.print(criteriaGrades[counter] + " / " + criteriaMaxGrade);
			fileOutput.println();
			fileOutput.println(criteria.getCriteriaDescription());
			fileOutput.println(SEPARATELINE);
			
			AssessmentCriteriaValueFacade.createNewCriteriaValue(criteriaID, assessmentID, criteriaGrades[counter]);
			
			counter ++;
		}
	}

	private static String formatDate(Timestamp timestamp)
	{
        if (timestamp == null) 
        {
            return null;
        }
        else 
        {
            return DATEFORMAT.format((java.util.Date) timestamp);
        }
	}	
	
	public void close()
	{
		try
		{
			fileOutput.close();
		}
		finally
		{
			fileOutput = null;
		}
	}
}